package com.sicau.socialpracticesystem.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * LocalDateTime类型转换工具
 */
public class LocalDateTimeUtils {

    /**
     * string2LocalDate
     * @param data data
     * @return return
     */
    public static LocalDate string2LocalDate(String data){
        return LocalDate.parse(data, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * string2LocalDateTime
     * @param data data
     * @return return
     */
    public static LocalDateTime string2LocalDateTime(String data){
        return LocalDateTime.parse(data, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * localDateTime2Date
     * @param localDateTime localDateTime
     * @return return
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    /**
     * localDate2Date
     * @param localDate localDate
     * @return return
     */
    public static Date localDate2Date(LocalDate localDate){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    /**
     * 计算两个日期的间隔，单位：天
     * @param localDate1 localDate1
     * @param localDate2 localDate2
     * @return return
     */
    public static long until(LocalDate localDate1,LocalDate localDate2){
        return localDate1.until(localDate2, ChronoUnit.DAYS);
    }
}
